#include <iostream>
#include "problem.h"
using namespace std;
//5 5
//01111
//01111
//01000
//01010
//00010

//10 10
//0111100110
//0001101010
//1000000000
//1001110001
//0000001100
//1111101100
//0010000010
//0001010110
//0110001110
//0100110010

//20 20
//01100100001000100100
//00000010111101000001
//01000111001000000001
//00100010011101010001
//01100001001111000101
//10000001111000000100
//00010000001100100101
//01100101001000001111
//00100101100010111111
//01100010000000000001
//10011011000101101111
//01101100110000100001
//00000011111110010100
//01111101110000100101
//01010000100011000111
//11000100101101010000
//01100001110000000100
//00001011110011000010
//01111011110111010100
//10000111110110001100
int main() {

    int row = 0, col = 0;
    int step = 0; // 完成步数

    cin >> row >> col;
    step = row + col - 2; // 距步数从曼哈顿距离开始算起

    Problem p(row, col, step);
    p.initMatrix();
    //cout << "init OK, Matrix:\n";
    p.showMatrix(0,0);

    p.genClauses();
    //cout << "genClause OK\n";
    p.saveCNF("test");

    while( step < row * col ){
        if(p.getResult()){
            // 打印相关信息，解析答案，还原路径
            p.printInfo();
            std::cout << "get result OK, parsing...\n";
            p.parseResult();
            //showMoving(res);
            return 0;
        }else{
            // 逐渐增加step，重置problem
            step += 1;
            p.resetStep(step);
            p.genClauses();
            p.saveCNF("test");
        }
    }

    return 0;
}

